/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package verification;

import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;

/**
 *
 * @author NATHAN
 */
public class PhoneFieldVerifier extends AbstractValidator {

  private int length;
  boolean canBeNull;

  public PhoneFieldVerifier(JPanel parent, JComponent c, String message, int length) {
    super(parent, c, message);
    this.length = length;
    canBeNull = false;
  }

  public PhoneFieldVerifier(JPanel parent, JComponent c, String message, int length, boolean canBeNull) {
    this(parent, c, message, length);
    this.canBeNull = canBeNull;
  }

  @Override
  protected boolean validationCriteria(JComponent c) {
    String number = ((JFormattedTextField) c).getText().replaceAll("[^0-9]", "");
    if (number.length() == 0 && canBeNull) {
      return true;
    }
    System.out.println("Phone length: " + number.length());
    if (number.length() != length) {
      setMessage("Phone number must contain 10 digits.");
      ((JFormattedTextField) c).selectAll();
      return false;
    }
    return true;
  }
}
